<?php


namespace app\Exception;


use think\exception\Handle;
use think\exception\HttpException;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\facade\Env;
use think\Response;
use Throwable;

class AppException extends Handle
{
    public function render($request, Throwable $e): Response
    {
        // 参数验证错误
        if ($e instanceof ValidateException) {
            // User相关的错误
            if ($e instanceof UserException) {
                return errorJson($e->getErrorCode(), $e->getMessage());
            }
            return errorJson(10001, $e->getError());
        }

        // 请求异常
        if ($e instanceof HttpException) {
            return errorJson(20001, $e->getMessage());
        }

        // 请求异常
        if ($e instanceof DbException) {
            return errorJson(40001, $e->getMessage());
        }

        // 其他错误交给系统处理
        if(Env::get('app_debug')){
            return parent::render($request, $e);
        }
        return errorJson(30001, $e->getMessage());

    }

}
